#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "mitar.h"

char uso[]="Uso: tar -c|x|r|t|-a nuevo_tar -f archivo_mtar [fich1 fich2 ...]\n";

int main(int argc, char *argv[]) {

	int opt, nExtra, retCode=EXIT_SUCCESS;
	flags flag=NONE;
	char *tarName=NULL;
	char *newtarName=NULL;
	//FILE *tarFile, **inputFiles;
	//headerEntry *header;

	//Menor n�mero de argumentos m�nimo v�lido=3: mitar -tfFile.tar
	if(argc < 2){
		fprintf(stderr, uso);
		exit(EXIT_FAILURE);
	}
	//Parseamos los flags
	while((opt = getopt(argc, argv, "cxtra:f:")) != -1) {
		switch(opt) {
			case 'c':
				flag=(flag==NONE)?CREATE:ERROR;
				break;
			case 't':
				flag=(flag==NONE)?LIST:ERROR;
				break;
			case 'x':
				flag=(flag==NONE)?EXTRACT:ERROR;
				break;
			case 'r':
				flag=(flag==NONE)?APPEND:ERROR;
				break;
			case 'a':
				flag=(flag==NONE)?APPENDNEW:ERROR;
				newtarName = optarg;
				break;
			case 'f':
				tarName = optarg;
				break;
			default:
				flag=ERROR;
		}
		//�Hemos detectado un fla v�lido?
		if(flag==ERROR){
			fprintf(stderr, "flag error\n");
			fprintf(stderr, uso);
			exit(EXIT_FAILURE);
		}
	}

	//Flag valido + argumento + fichero[s]
	if(flag==NONE || tarName==NULL) {
		fprintf(stderr,"no flag or tarname\n");
		fprintf(stderr, uso);
		exit(EXIT_FAILURE);
	}

	//Flag valido + argumento + fichero[s]
	if(flag==APPENDNEW && newtarName==NULL) {
		fprintf(stderr,"need new filename\n");
		fprintf(stderr, uso);
		exit(EXIT_FAILURE);
	}

	//#argumentos extra
	nExtra=argc-optind;

	//Ejecutamos la accion requerida
	switch(flag) {
		case CREATE:
			retCode=createTar(nExtra, &argv[optind], tarName);
			break;
		case LIST:
			if(nExtra!=0){
				fprintf(stderr, uso);
				exit(EXIT_FAILURE);
			}
			//retCode=listTar(tarName);
			break;
		case EXTRACT:
			if(nExtra!=0){
				fprintf(stderr, uso);
				exit(EXIT_FAILURE);
			}
			retCode=extractTar(tarName);
			break;
		case APPEND:
			//retCode=appendTar(nExtra, &argv[optind], tarName);
			break;
		case APPENDNEW:
			retCode=appendnewTar(nExtra, &argv[optind], tarName, newtarName);
			break;
		default:
			retCode=EXIT_FAILURE;
	}
	exit(retCode);

}
